import { isStartWithNum, hasLetter } from './check';

/**
 * 调用localStorage 存储方法
 * @param key
 * @param value
 * @param fn 成功回调
 * @param error
 */
export const setLocalStorage = (key, value, fn = () => { }, error = () => { }) => {
    if (!key) return error(new Error('LocalStorage must has a param key'));
    if (isStartWithNum(key)) return error(new Error('LocalStorage param key must contains letter'));
    if (typeof value === 'object') return error(new Error('LocalStorage param value type must not be an object'));
    if (window.localStorage) {
        try {
            window.localStorage.setItem(key, value);
            typeof fn === 'function' && fn();
        } catch (e) {
            return error(e);
        }

    } else {
        return error(new Error('Your browser does not support localStorage'));
    }
};


/**
 * 移除本地存储中的某个字段
 * @param key
 * @param error
 * @returns {*}
 */
export const removeLocalStorage = (key, error = () => { return '' }) => {
    if (!key) return error(new Error('LocalStorage must has a param key'));
    if (!hasLetter(key)) return error(new Error('LocalStorage param key must contains letter'));
    if (window.localStorage) {
        return window.localStorage.removeItem(key);
    } else {
        return error(new Error('Your browser does not support localStorage'));
    }
};

/**
 * 获取本地存储
 * @param key 
 * @param error
 * @returns {*}
 */
export const getLocalStorage = (key, error = () => { return '' }) => {
    if (!key) return error(new Error('LocalStorage must has a param key'));
    if (!hasLetter(key)) return error(new Error('LocalStorage param key must contains letter'));
    if (window.localStorage) {
        return window.localStorage.getItem(key);
    } else {
        return error(new Error('Your browser does not support localStorage'));
    }
};

export const cookieManager = new (class CookieManager {
    cookie = {};
  
    constructor() {
      const ca = document.cookie.split(';'); // return 'the "username";username; time'
      for (let i = 0; i < ca.length; i++) {
        const [key, val] = ca[i].trim().split('=');
        try {
          this.cookie[key] = JSON.parse(val);
        } catch (e) {
          this.cookie[key] = val;
        }
      }
    }
  
    getCookie(k) {
      return this.cookie[k];
    }
  
    getAll() {
      return this.cookie;
    }
  
    setCookie(
      k,
      v,
      opt = {},
    ) {
      this.cookie[k] = v;
      if (typeof v === 'object' && v !== null) {
        // eslint-disable-next-line no-param-reassign
        v = JSON.stringify(v);
      }
      let str = '';
      if (!opt.path) {
        str = '; path=/';
      }
      // eslint-disable-next-line no-restricted-syntax
      for (const prop of Object.keys(opt)) {
        str += `;${prop}=${opt[prop]}`;
      }
      document.cookie = `${k}=${v}; ${str}`;
    }
  
    delCookie(k) {
      const myDate = new Date();
      myDate.setTime(-1000); // 设置时间
      document.cookie = `${k}=''; expires=${myDate.toUTCString()}`;
      this.cookie[k] = null;
      // this.setCookie(k, "")
    }
  })();